﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views {
    import flash.events.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.views.components.*;
    import net.wooga.pets.constants.*;

    public class VillageMediator extends RenderListMediator {

		[Inject]
        public var village:Village;
		[Inject]
        public var gameStateModel:GameStateModel;
		[Inject]
        public var userModel:UserModel;
		[Inject]
        public var friendsModel:VisitingFriendModel;
        private var _cachedPetData:Array;

        public function VillageMediator(){
            this._cachedPetData = [];
            super();
        }
        override public function onRegister():void{
            super.onRegister();
            eventMap.mapListener(eventDispatcher, EventConstants.START_IN_VILLAGE, this.onStartInVillage);
            eventMap.mapListener(eventDispatcher, EventConstants.ON_HOSPITAL_LEFT, this.onHospitalLeft);
            eventMap.mapListener(eventDispatcher, GameEvent.LEVEL_UP, this.onLevelUp);
            eventMap.mapListener(eventDispatcher, EventConstants.FRIENDS_MODEL_CHANGED, this.friendsModelChanged);
            eventMap.mapListener(eventDispatcher, EventConstants.DISABLE_VILLAGE, this.disableVillage);
            eventMap.mapListener(eventDispatcher, EventConstants.ENABLE_VILLAGE, this.enabledVillage);
            eventMap.mapListener(eventDispatcher, EventConstants.SHOW_FIREWORK, this.showFirework);
            eventMap.mapListener(eventDispatcher, EventConstants.SEND_AMBULANCE_AWAY, this.onSendAmbulanceAway);
            eventMap.mapListener(eventDispatcher, EventConstants.SHOW_VILLAGE_ANIMATION, this.showVillageAnimation);
            eventMap.mapListener(eventDispatcher, EventConstants.HIDE_VILLAGE_ANIMATION, this.hideVillageAnimation);
            this.enabledVillage();
            this.updateVillageData();
        }
        private function enabledVillage(_arg1:Event=null):void{
            eventMap.mapListener(this.village, EventConstants.ON_ARRIVAL_AT_HOSPITAL, this.onArrivalAtHospital);
            eventMap.mapListener(this.village, EventConstants.ON_ARRIVAL_AT_VILLAGE, this.onArrivalAtVillage);
        }
        private function disableVillage(_arg1:Event):void{
            eventMap.unmapListener(this.village, EventConstants.ON_ARRIVAL_AT_HOSPITAL, this.onArrivalAtHospital);
            eventMap.unmapListener(this.village, EventConstants.ON_ARRIVAL_AT_VILLAGE, this.onArrivalAtVillage);
        }
        private function onSendAmbulanceAway(_arg1:Event):void{
            this.village.sendAmbulanceAway();
        }
        private function friendsModelChanged(_arg1:Event):void{
            this.village.setLevel(int(this.friendsModel.level));
            this.village.setNumberOfCuredPets(this.friendsModel.numberOfCuredPets);
        }
        private function onLevelUp(_arg1:InfoEvent):void{
            this.village.setLevel(this.userModel.level);
        }
        private function showFirework(_arg1:Event):void{
            this.village.showFireworks();
        }
        private function updateVillageData():void{
            this.village.setLevel(int(this.userModel.level));
            this.village.setNumberOfCuredPets(this.userModel.numberOfCuredPets);
        }
        private function onHospitalLeft(_arg1:InfoEvent):void{
            var _local2:String = _arg1.id;
            this.village.addPetToLeavingHospitalQueue(_local2);
            this.village.playPetLeavesHospital();
        }
        private function onStartInVillage(_arg1:InfoEvent):void{
            this._cachedPetData.push(_arg1.info);
            this.village.addPetToLeavingVillageQueue(_arg1.id);
            this.village.playPetLeavesVillage();
        }
        private function onArrivalAtHospital(_arg1:InfoEvent):void{
            dispatch(new InfoEvent(EventConstants.CREATE_PATIENT, null, this._cachedPetData.shift()));
            dispatch(new Event(EventConstants.ON_ARRIVAL_AT_HOSPITAL));
            dispatch(new InfoEvent(EventConstants.PLAY_EFFECT_SOUND, GameConstants.SOUND_DOOR_CLOSES));
        }
        private function onArrivalAtVillage(_arg1:InfoEvent):void{
            this.village.setNumberOfCuredPets(int(this.userModel.numberOfCuredPets));
        }
        private function hideVillageAnimation(_arg1:Event):void{
            this.village.setInOutAnimationVisible(false);
        }
        private function showVillageAnimation(_arg1:Event):void{
            this.village.setInOutAnimationVisible(true);
        }

    }
}//package net.wooga.pets.views 
